#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2011 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##

from .pathdb import PATH_DB
from ..viewlib.dialog import Dialog


def __load_mallard_doc(document, altdirs):
    """
    __load_mallard_doc(document:str) -> bool/None

    Handle loading Mallard help.

    Launch Mallard docs with default handler. Passed **document** must the
    reference to load with no extension. The reference will have 'game.{}' set
    for any reference not 'index', or 'howtoplay'. Documents for 'news' and
    'license' will be ignored.
    """
    result = None
    try:
        from gio import File
        helpfile = 'ghelp://{}'.format(PATH_DB.find_dir('page/C', altdirs))
        if not document in ['news', 'license']: # FIX-ME: need pages for these.
            if not document == 'index':
                helpfile = '{0}#game.{1}'.format(helpfile, document)
            gfile = File(helpfile)
            handler = gfile.query_default_handler(None)
            return handler.launch([gfile], None)
    except:
        pass


def __load_browser_doc(document, altdirs):
    """
    _load_browser_doc(document:str) -> bool/None

    Handle loading html help.

    Launch default browser to show HTML docs. Passed document value must the
    file to load without extension. Files will have '{}.html' set. Files not
    'news', 'license', 'index', or 'howtoplay' will set 'rules/{}' also.
    """
    result = None
    try:
        from .utillib.misc import openURL
        locate = 'html/{}.html'
        if not document in ['news', 'license', 'index', 'howtoplay']:
            locate = locate.format('rules/{}')
        helpfile = PATH_DB.find_file(locate.format(document),
                                         subdirs=altdirs)
        return openURL(helpfile)
    except:
        pass


def load_help(document):
    """
    load_help(document:str) -> None

    Handle loading help.

    First calls **__load_mallard_doc**. If that returns None, calls
    **__load_browser_doc**. Otherwise it will show warning dialog about not
    being able to load document.
    """
    altdirs = ['../docs']
    result = __load_mallard_doc(document, altdirs)
    if not result:
        result = __load_browser_doc(document, altdirs)
        if not result:
            Dialog(text=_("Cannot load {} document.\n").format(document))